package com.faxsun.controller.uc.entity;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 第三方帐户上下文
 * 
 * @author Xinyin.Xiao
 *
 */
public class ThirdPartyContext {
	private static final String FAIL = "redirect:/";

	private Map<String, ThirdPartyLoginAbs> sourceList;

	public Map<String, ThirdPartyLoginAbs> getSourceList() {
		return sourceList;
	}

	public void setSourceList(Map<String, ThirdPartyLoginAbs> sourceList) {
		this.sourceList = sourceList;
	}

	public String loginContext(String channel, String path, HttpServletRequest request, HttpServletResponse response) {
		if (sourceList.containsKey(channel)) {
			ThirdPartyLoginAbs abs = sourceList.get(channel);
			ThirdPartyObj obj3rd = abs.get3rdObj(request, response, channel, path);
			return abs.login(obj3rd);
		} else {
			return FAIL;
		}
	}

	public void responseContext(String channel, HttpServletRequest request, HttpServletResponse response) {
		if (sourceList.containsKey(channel)) {
			ThirdPartyLoginAbs abs = sourceList.get(channel);
			abs.ResponseURL(request, response);
		}
	}

	/**
	 * 绑定请求上下文处理,查询第三方处理类，调用对应的方法，构造回调url，并跳转到第三方进行验证
	 * 
	 * @param channel
	 * @param request
	 * @param response
	 */
	public void requestBindContext(String channel, HttpServletRequest request, HttpServletResponse response) {
		if (sourceList.containsKey(channel)) {
			ThirdPartyLoginAbs abs = sourceList.get(channel);
			abs.bindRequestUrl(request, response);
		}
	}

	/**
	 * 解析第三方帐户信息
	 * @param channel
	 * @param path
	 * @param request
	 * @param response
	 * @return
	 */
	public ThirdPartyObj parseThirdAccount(String channel, HttpServletRequest request,
			HttpServletResponse response) {
		if (sourceList.containsKey(channel)) {
			ThirdPartyLoginAbs abs = sourceList.get(channel);
			ThirdPartyObj obj3rd = abs.get3rdObj(request, response, channel, null);
			return obj3rd;
		} else {
			return null;
		}
	}

}
